<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/base/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>${systemOption.systemTitle}</title>
    <%@ include file="/base/head.jsp"%>
    <script type="text/javascript" charset="UTF-8">
	$(function(){
    		$('#typetree').tree({
    			onClick: function(node){
		    			$("#valueList").datagrid({title:' '+node.text});			    					    			
		    			$('#valueList').datagrid({url:'${ctx}/json/listDicvalue.action?id='+node.id});
		    			$('#valueList').datagrid('load');
		    			$('#addValueId').validatebox({   
                                          min:1,
                                          required:true,   
                                          validType:'remote["${ctx}/json/checkUniqueDicTV.action?tid='+node.id+'","typeValueCheck"]',
                                          invalidMessage:"类型值重复!"
                         });	    		
				}    			
    		});
    	
    	
    	})
    //增加
  	function add_type(){
  			$('#typeAddWnd').show();
            $('#typeAddWnd').dialog({
            	iconCls:'icon-add',
            	title:'增加字典类型',
            	modal:true,
            	buttons: [{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					$('#typeAddForm').form('submit',{ 
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','增加成功','info'); 
					            $('#typetree').tree('reload');
					        }else{
					        	$.messager.alert('友情提示','增加失败','error'); 
					        } 
					        $('#typeAddWnd').dialog('close');
					    }
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#typeAddWnd').dialog('close');
				}
			}]
         });    
  		$('#typeAddForm').form('reset');
  		$('#firstBox').focus();  
  	}
  //修改
  function edit_type(){
  	var node = $('#typetree').tree('getSelected');
	if (!node){
		$.messager.alert('友情提示','请选择字典','error');
	}else{
		$('#typeUpdateWnd').show();
		$('#typeUpdateWnd').dialog({
        iconCls:'icon-edit',
        title:'修改字典类型',
        modal:true,
        buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					$('#typeUpdateForm').form('submit',{
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','修改成功','info');
					            $('#typetree').tree('reload');
					        }else{
					        	$.messager.alert('友情提示','修改失败','error');
					        }
					        $('#typeUpdateWnd').dialog('close');
					    }
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#typeUpdateWnd').dialog('close');
				}
		}]
            });
            //加载数据
            $.post("${ctx}/json/loadDictype.action", {"id": node.id },
			   function(data){
			    if(data){
					$('#typeId').val(data.id);
					$('#typevalue').val(data.value);
					$('#typetext').val(data.text);
					$('#typesortnum').numberspinner('setValue',data.sortnum);
					$('#typebak').val(data.bak);
				}else{
					$.messager.alert('友情提示','加载失败','error');
				}
			   }, "json");


		}
	}
	function remove_type(){
		var node = $('#typetree').tree('getSelected');
		if (!node){
			$.messager.alert('友情提示','请选择字典','error');
		}else{
			$.messager.confirm('友情提示','将删除该字典及所有的字典值，确定么？',function(r){   
				    if (r){   
				       $.post("${ctx}/json/removeDictype.action", {"id": node.id },
					   function(data){
					    if(data==true){  
							$.messager.alert('友情提示','删除成功','info'); 
							$("#valueList").datagrid({title:'数据字典 '});
							$('#valueList').datagrid('loadData', { total: 0, rows: [] });
							$('#typetree').tree('reload');
						}else{
							$.messager.alert('友情提示','删除失败','error'); 
						}
					   }, "json");   
				    }   
				});
		}
	
	}
	//增加字典值
	function add_value(){
		var node=$('#typetree').tree('getSelected');
		if(node!=null){
	    		$('#valueAddWnd').show();
	    		$('#valueAddWnd').dialog({
	    			title: '增加值', 
					modal:true,
					buttons:[{
						text:'确定',
						iconCls:'icon-ok',
						handler:function(){
							$('#valueAddForm').form('submit',{
							url:'${ctx}/json/saveDicvalue.action?tid='+node.id,
					    	success: function(data){
						        if(data=='true'){
						            $.messager.alert('友情提示','增加成功','info');						            
						        }else{
						        	$.messager.alert('友情提示','增加失败','error');
						        }
						        $('#valueAddWnd').dialog('close');
						        $('#valueList').datagrid('reload');
					    	}
					});
						}
					},{
						text:'取消',
						iconCls:'icon-cancel',
						handler:function(){$('#valueAddWnd').dialog('close')}
					}]
	    		});
	    		$('#valueAddForm').form('reset');
    	}else{
    			$.messager.alert('友情提示','请选择字典','error');
    	}
	
	}
  //修改字典值
  function edit_value(){
  	var row = $('#valueList').datagrid('getSelected');
	if (!row){
		$.messager.alert('友情提示','请选择字典值','error');
	}else{
		$('#valueUpdateWnd').show();
		$('#valueUpdateWnd').dialog({
        iconCls:'icon-edit',
        title:'修改字典类值',
        modal:true,
        buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					$('#valueUpdateForm').form('submit',{
						url:'${ctx}/json/saveDicvalue.action',
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','修改成功','info');
					            $('#valueList').datagrid('load');
					        }else{
					        	$.messager.alert('友情提示','修改失败','error');
					        }
					        $('#valueUpdateWnd').dialog('close');
					    }
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#valueUpdateWnd').dialog('close');
				}
		}]
            });
            //加载数据
			$('#valuevid').val(row.vid);
			$('#valuetext').val(row.text);
			$('#valueid').val(row.id);
			$('#valuesortnum').numberspinner('setValue',row.sortnum);
			$('#valuestatus').combobox('setValue',row.status);
			$('#valuebak').val(row.bak);
		}
	}
	function remove_value(){
		var row = $('#valueList').datagrid('getSelected');
		if (!row){
			$.messager.alert('友情提示','请选择字典值','error');
		}else{
			$.messager.confirm('友情提示','将删除该字典值，确定么？',function(r){   
				    if (r){   
				       $.post("${ctx}/json/removeDicvalue.action", {"vid": row.vid },
					   function(data){
					    if(data==true){  
							$.messager.alert('友情提示','删除成功','info'); 
							$('#valueList').datagrid('load');
						}else{
							$.messager.alert('友情提示','删除失败','error'); 
						}
					   }, "json");   
				    }   
				});
		}
	
	}
	//根据不同值，显示不同内容
	function formatStatus(val,row){
		if (val == 1){
			return '<img src="${ctx}/images/checkmark.gif"/>';
		}else if (val == 0){
			return '<img src="${ctx}/images/checknomark.gif"/>';
		} else {
			return '无';
		}
	}
    </script>
  </head>
  
  <body>
    <div class="easyui-layout" fit="true">
		<div data-options="region:'west',split:true" style="width:200px;border:0" >
			<div id="p" class="easyui-panel" title="字典类别" data-options="fit:true,iconCls:'icon-book-red'">
				<div class="toolbar">
					<a href="javascript:void(0)" onclick="add_type()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">增加</a>
					<a href="javascript:void(0)" onclick="edit_type()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
					<a href="javascript:void(0)" onclick="remove_type()" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true">删除</a>				
				</div>
				<ul id="typetree" class="easyui-tree" data-options="url:'${ctx}/json/listDictype.action'"></ul>
			</div>		
		</div>
		<div data-options="region:'center'" style="border:0">
			<table id="valueList" class="easyui-datagrid" title="数据字典" 
				data-options="fit:true,singleSelect:true,toolbar:'#tb',fitColumns:true">
				<thead>
					<tr>
						<th data-options="field:'ck',checkbox:true"></th>
						<th data-options="field:'text',width:150">文本值</th>
						<th data-options="field:'id',width:100">值</th>
						<th data-options="field:'status',width:80,formatter:formatStatus">状态</th>
						<th data-options="field:'sortnum',width:80">排序</th>
						<th data-options="field:'bak',width:250">备注</th>
						<th data-options="field:'vid',hidden:true"></th>
					</tr>
				</thead>
			</table>		
		</div>
	</div>
	<!-- Datagrid工具栏 -->
	<div id="tb">
		<table width="100%" cellpadding="0" cellspacing="0">
		<tr>
		   <td class="toolDiv">  		
		  		<a href="javascript:void(0)" onclick="add_value()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">增加值</a><span class="vline">|</span>
				<a href="javascript:void(0)" onclick="edit_value()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改值</a><span class="vline">|</span>			
				<a href="javascript:void(0)" onclick="remove_value()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除值</a>
			</td>
		</tr>
		</table>
	</div> 
	<!-- 增加字典类型窗口 -->
	<div id="typeAddWnd"  style="width: 400px; height: 300px; padding: 5px; background: #fafafa;display: none">
    	<form id="typeAddForm" method="post" action="${ctx }/json/saveDictype.action">
                <table cellpadding=3>                                        
                	<tr>
                        <td style="width: 100px;">类型名：</td>
                        <td style="width: 200px;"><input id="firstBox" name="text" class="easyui-validatebox"  data-options="required:true" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>类型值：</td>
                        <td><input  name="value" class="easyui-validatebox" data-options="required:true" validType="remote['${ctx}/json/checkDictypeValue.action','typeValueCheck']" invalidMessage="类型值重复!" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>排序：</td>
                        <td><input name="sortnum" class="easyui-numberspinner"  data-options="value:0,min:0,max:500,editable:false" style="width:225px"/></td>
					</tr>
					
                    <tr>
                        <td>备注：</td>
                        <td><textarea name="bak" cols="33" rows="3"></textarea>
                    </tr>                    				                      
                </table>
        </form>
	</div>
   <!--修改字典类型窗口-->
    <div id="typeUpdateWnd"  style="width: 400px; height: 300px; padding: 5px; background: #fafafa;display: none">
    	<form id="typeUpdateForm" method="post" action="${ctx }/json/saveDictype.action">
    	<input id="typeId" name="id" type="hidden"/>
                <table cellpadding=3>                                    
                	<tr>
                        <td style="width: 100px;">类型名：</td>
                        <td style="width: 200px;"><input id="typetext" name="text" class="easyui-validatebox"  data-options="required:true" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>类型值：</td>
                        <td><input id="typevalue" name="value" class="easyui-validatebox" data-options="required:true" readonly="readonly" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>排序：</td>
                        <td><input id="typesortnum" name="sortnum" class="easyui-numberspinner"  data-options="value:0,min:0,max:500,editable:false" style="width:225px"/></td>
					</tr>
                    <tr>
                        <td>备注：</td>
                        <td><textarea id="typebak" name="bak" cols="33" rows="3"/></textarea>
                    </tr>                    				                      
                </table>
        </form>
    </div> 
    <!-- 增加字典值窗口 -->  
    <div id="valueAddWnd"  style="width: 400px; height: 300px; padding: 5px; background: #fafafa;display: none">
    	<form id="valueAddForm" method="post">
                <table cellpadding=3>                                     
                	<tr>
                        <td style="width: 100px;">文本值：</td>
                        <td style="width: 200px;"><input name="text" class="easyui-validatebox"  data-options="required:true" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>值：</td>
                        <td><input id="addValueId"  name="id"  style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>排序：</td>
                        <td><input name="sortnum" class="easyui-numberspinner"  data-options="value:0,min:0,max:500,editable:true" style="width:225px"/></td>
					</tr>
					<tr>
                        <td>状态：</td>
                        <td>
                        	<select class="easyui-combobox" name="status" style="width:225px">  
							    <option value="1">启用</option>
							    <option value="0">禁用</option>
							</select>
                        </td>
					</tr>
                    <tr>
                        <td>备注：</td>
                        <td><textarea name="bak" cols="33" rows="3"/></textarea>
                    </tr>                    				                      
                </table>
        </form>
	</div>
	<!-- 修改字典值窗口 -->  
    <div id="valueUpdateWnd"  style="width: 400px; height: 300px; padding: 5px; background: #fafafa;display: none">
    	<form id="valueUpdateForm" method="post">
    	<input id="valuevid" name="vid" type="hidden"/>
                <table cellpadding=3>                                        
                	<tr>
                        <td style="width: 100px;">文本值：</td>
                        <td style="width: 200px;"><input id="valuetext" name="text" class="easyui-validatebox"  data-options="required:true" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>值：</td>
                        <td><input id="valueid" name="id"  class="easyui-validatebox" readonly="readonly" style="width:220px"/></td>
                    </tr>
                    <tr>
                        <td>排序：</td>
                        <td><input id="valuesortnum" name="sortnum" class="easyui-numberspinner"  data-options="value:0,min:0,max:500,editable:true" style="width:225px"/></td>
					</tr>
					<tr>
                        <td>状态：</td>
                        <td>
                        	<select id="valuestatus" class="easyui-combobox" name="status" style="width:225px">  
							    <option value="1">启用</option>
							    <option value="0">禁用</option>
							</select>
                        </td>
					</tr>
                    <tr>
                        <td>备注：</td>
                        <td><textarea id="valuebak" name="bak" cols="33" rows="3"/></textarea>
                    </tr>                    				                      
                </table>
        </form>
	</div>
     
  </body>
</html>
